/*
 * TemplatePropertyView.java
 *
 * Created on 13. Juni 2008, 13:04
 */

package twi.query.template2.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JCheckBox;

import twi.Utils;
import twi.query.template2.PropertyNode;


/**
 *
 * @author  Soeren
 */
public class TemplatePropertyView extends javax.swing.JPanel {

    private PropertyChangeListener pcl;
    private ArrayList<DatatypePropertyPanel> proppanels = new ArrayList<DatatypePropertyPanel>();
    
    
	/** Creates new form TemplatePropertyView 
     * @param templateClassNode 
     * @param hashMap */
    public TemplatePropertyView(boolean edit, HashMap<String, PropertyNode> propMap, PropertyChangeListener pcl) {
        initComponents();
        this.pcl = pcl;
        setPropertys(edit, propMap);
    }


	private void setPropertys(boolean edit, HashMap<String, PropertyNode> propMap) {
		this.propertyPanel.removeAll();
		this.propertyPanel.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.gridx = 0;gc.gridy = 0;
		gc.gridheight = 1; gc.gridwidth = 1;
		gc.anchor = GridBagConstraints.WEST;
		Iterator<PropertyNode> iter = propMap.values().iterator();
		while(iter.hasNext()){
			PropertyNode p = iter.next();
			final String propertyName = p.propertyName;
			boolean isDatatypeProperty = Utils.isDatatypeProperty(propertyName);
			p.datatypeProperty = isDatatypeProperty;
			if (isDatatypeProperty){
				DatatypePropertyPanel pp = new DatatypePropertyPanel(edit, propertyName, pcl);
				proppanels.add(pp);
				this.propertyPanel.add(pp, gc);
				gc.gridy++;
				continue;
			}else{
				final JCheckBox jc = new JCheckBox(propertyName);
				jc.setEnabled(edit);
				if (edit)
					jc.setSelected(false);
				jc.addItemListener(new ItemListener(){
					@Override
					public void itemStateChanged(ItemEvent e) {
						pcl.changeProperty(jc.isSelected(), propertyName);
					}						
				});
				jc.setVisible(true);
				propertyPanel.add(jc, gc);
				
				JButton b = new JButton("add");
				b.addActionListener(new ActionListener(){

					@Override
					public void actionPerformed(ActionEvent e) {
						pcl.changeProperty(true, propertyName);
					}
					
				});
				gc.gridx++;
				this.propertyPanel.add(b, gc);
				gc.gridx--;
				
				gc.gridy++;
			}
		}
	}


	/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        propertyPanel = new javax.swing.JPanel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(twi_project.TWI_ProjectApp.class).getContext().getResourceMap(TemplatePropertyView.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(10);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setText(resourceMap.getString("jTextArea1.text")); // NOI18N
        jTextArea1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jTextArea1.border.title"))); // NOI18N
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        propertyPanel.setName("propertyPanel"); // NOI18N

        javax.swing.GroupLayout propertyPanelLayout = new javax.swing.GroupLayout(propertyPanel);
        propertyPanel.setLayout(propertyPanelLayout);
        propertyPanelLayout.setHorizontalGroup(
            propertyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 172, Short.MAX_VALUE)
        );
        propertyPanelLayout.setVerticalGroup(
            propertyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 217, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(propertyPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JPanel propertyPanel;
    // End of variables declaration//GEN-END:variables
	public String getPropertyValue(String prop) {
		for (int i = 0; i < proppanels.size(); i++)
			if (proppanels.get(i).isProperty(prop))
				return proppanels.get(i).getValue();
		return null;
	}

}
